import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isUndefined from "lodash/isUndefined";
import _isFunction from "lodash/isFunction";
import _isEmpty from "lodash/isEmpty";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _dec11, _class, _class2;

import React from 'react';
import { Button, Form, Input, Modal, notification, Table } from 'antd';
import { Bind } from 'lodash-decorators';
import Lov from 'components/Lov';
import intl from 'utils/intl';
import { createPagination, tableScrollWidth } from 'utils/utils';
var FormItem = Form.Item;
var rowProps = {
  style: {
    cursor: 'pointer'
  }
};
/**
 * RoleModal-选择新的角色 弹框
 * @reactProps {Boolean} visible 模态框是否显示
 * @reactProps {Boolean} dataSource 角色的数据源
 * @reactProps {Function(selectedRows:Object[]):Promise|*} onSave 确认按钮的回调,接收选中的角色,返回一个Promise对象或者任意值
 * @reactProps {Function} onCancel 取消按钮的回调
 */

var RoleModal = (_dec = Form.create({
  fieldNameProp: null
}), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec10 = Bind(), _dec11 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_React$PureComponent) {
  _inherits(RoleModal, _React$PureComponent);

  var _super = _createSuper(RoleModal);

  function RoleModal(props) {
    var _this;

    _classCallCheck(this, RoleModal);

    _this = _super.call(this, props);
    _this.state = {
      selectedRowKeys: [],
      selectedRows: [],
      dataSource: [],
      pagination: false
    };
    return _this;
  }

  _createClass(RoleModal, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.fetchRoleList();
    }
  }, {
    key: "handleSearchBtnClick",
    value: function handleSearchBtnClick() {
      this.fetchRoleList();
    }
  }, {
    key: "fetchRoleList",
    value: function fetchRoleList() {
      var _this2 = this;

      var pagination = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
        page: 0,
        size: 10
      };
      var _this$props = this.props,
          fetchRoles = _this$props.fetchRoles,
          form = _this$props.form,
          _this$props$excludeRo = _this$props.excludeRoleIds,
          excludeRoleIds = _this$props$excludeRo === void 0 ? [] : _this$props$excludeRo,
          _this$props$excludeUs = _this$props.excludeUserIds,
          excludeUserIds = _this$props$excludeUs === void 0 ? [] : _this$props$excludeUs;

      var queryParams = _objectSpread(_objectSpread({}, pagination), {}, {
        excludeRoleIds: excludeRoleIds,
        excludeUserIds: excludeUserIds
      });

      var name = form.getFieldValue('name');
      var tenantId = form.getFieldValue('tenantId');

      if (!_isUndefined(name)) {
        queryParams.name = name;
      }

      if (!_isUndefined(tenantId)) {
        queryParams.tenantId = tenantId;
      }

      this.setState({
        fetchRolesLoading: true
      });
      fetchRoles(queryParams).then(function (res) {
        if (res) {
          _this2.setState({
            dataSource: res.content,
            pagination: createPagination(res),
            selectedRowKeys: [],
            selectedRows: []
          });
        }
      }).finally(function () {
        _this2.setState({
          fetchRolesLoading: false
        });
      });
    }
  }, {
    key: "handleRow",
    value: function handleRow() {
      return rowProps;
    }
  }, {
    key: "handleRowClick",
    value: function handleRowClick(record) {
      var _this$state = this.state,
          _this$state$selectedR = _this$state.selectedRowKeys,
          selectedRowKeys = _this$state$selectedR === void 0 ? [] : _this$state$selectedR,
          _this$state$selectedR2 = _this$state.selectedRows,
          selectedRows = _this$state$selectedR2 === void 0 ? [] : _this$state$selectedR2;

      if (selectedRowKeys.some(function (id) {
        return id === record.id;
      })) {
        // 已经选中 需要移除
        this.setState({
          selectedRowKeys: selectedRowKeys.filter(function (id) {
            return id !== record.id;
          }),
          selectedRows: selectedRows.filter(function (item) {
            return item !== record;
          })
        });
      } else {
        // 没有选中 需要新增
        this.setState({
          selectedRowKeys: [].concat(_toConsumableArray(selectedRowKeys), [record.id]),
          selectedRows: [].concat(_toConsumableArray(selectedRows), [record])
        });
      }
    }
  }, {
    key: "handleRowSelectionChange",
    value: function handleRowSelectionChange(_, selectedRows) {
      this.setState({
        selectedRowKeys: selectedRows.map(function (r) {
          return r.id;
        }),
        selectedRows: selectedRows
      });
    }
  }, {
    key: "handleTableChange",
    value: function handleTableChange() {
      var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
          _ref$current = _ref.current,
          current = _ref$current === void 0 ? 1 : _ref$current,
          _ref$pageSize = _ref.pageSize,
          pageSize = _ref$pageSize === void 0 ? 10 : _ref$pageSize;

      this.fetchRoleList({
        page: current - 1,
        size: pageSize
      });
    }
    /**
     * 保存
     */

  }, {
    key: "handleSaveBtnClick",
    value: function handleSaveBtnClick() {
      var selectedRows = this.state.selectedRows;
      this.save(selectedRows);
    }
    /**
     *
     * @param {Object[]} selectedRows
     */

  }, {
    key: "save",
    value: function save(selectedRows) {
      var onSave = this.props.onSave;

      if (_isEmpty(selectedRows)) {
        notification.warning({
          message: intl.get('ciam.subAccount.view.message.chooseNewRoleFirst').d('请先选择新增的角色')
        });
      } else if (_isFunction(onSave)) {
        onSave(selectedRows);
        this.setState({
          selectedRowKeys: [],
          selectedRows: []
        });
      }
    }
    /**
     * 取消保存
     */

  }, {
    key: "handleCancelBtnClick",
    value: function handleCancelBtnClick() {
      var onCancel = this.props.onCancel;

      if (_isFunction(onCancel)) {
        this.setState({
          selectedRowKeys: [],
          selectedRows: []
        });
        onCancel();
      }
    }
  }, {
    key: "handleResetBtnClick",
    value: function handleResetBtnClick() {
      var resetFields = this.props.form.resetFields;
      resetFields();
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          visible = _this$props2.visible,
          getFieldDecorator = _this$props2.form.getFieldDecorator;
      var _this$state2 = this.state,
          selectedRowKeys = _this$state2.selectedRowKeys,
          _this$state2$dataSour = _this$state2.dataSource,
          dataSource = _this$state2$dataSour === void 0 ? [] : _this$state2$dataSour,
          _this$state2$paginati = _this$state2.pagination,
          pagination = _this$state2$paginati === void 0 ? false : _this$state2$paginati,
          fetchRolesLoading = _this$state2.fetchRolesLoading;
      var columns = [{
        title: intl.get('ciam.subAccount.model.role.name').d('角色名称'),
        dataIndex: 'name',
        width: 200
      }, {
        title: intl.get('ciam.subAccount.model.role.tenantName').d('所属租户'),
        dataIndex: 'tenantName',
        width: 200
      }];
      return /*#__PURE__*/React.createElement(Modal, {
        visible: visible,
        onOk: this.handleSaveBtnClick,
        onCancel: this.handleCancelBtnClick,
        width: 720,
        title: intl.get('ciam.subAccount.view.message.title.roleModal').d('选择角色')
      }, /*#__PURE__*/React.createElement(Form, {
        layout: "inline"
      }, /*#__PURE__*/React.createElement(FormItem, {
        label: intl.get('ciam.subAccount.model.role.name').d('角色名称')
      }, getFieldDecorator('name')( /*#__PURE__*/React.createElement(Input, null))), /*#__PURE__*/React.createElement(FormItem, {
        label: intl.get('ciam.subAccount.model.user.tenant').d('所属租户')
      }, getFieldDecorator('tenantId')( /*#__PURE__*/React.createElement(Lov, {
        code: "CPFM.TENANT",
        textField: "tenantName"
      }))), /*#__PURE__*/React.createElement(FormItem, null, /*#__PURE__*/React.createElement(Button, {
        style: {
          marginRight: 8
        },
        onClick: this.handleResetBtnClick
      }, intl.get('cpaas.common.button.reset').d('重置')), /*#__PURE__*/React.createElement(Button, {
        htmlType: "submit",
        type: "primary",
        onClick: this.handleSearchBtnClick
      }, intl.get('cpaas.common.button.search').d('查询')))), /*#__PURE__*/React.createElement(Table, {
        bordered: true,
        rowKey: "id",
        loading: fetchRolesLoading,
        columns: columns,
        pagination: pagination,
        dataSource: dataSource,
        style: {
          marginTop: 14
        },
        scroll: {
          x: tableScrollWidth(columns)
        },
        onRow: this.handleRow,
        onRowClick: this.handleRowClick,
        rowSelection: {
          selectedRowKeys: selectedRowKeys,
          onChange: this.handleRowSelectionChange
        },
        onChange: this.handleTableChange
      }));
    }
  }]);

  return RoleModal;
}(React.PureComponent), (_applyDecoratedDescriptor(_class2.prototype, "handleSearchBtnClick", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSearchBtnClick"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "fetchRoleList", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "fetchRoleList"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleRow", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "handleRow"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleRowClick", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "handleRowClick"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleRowSelectionChange", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "handleRowSelectionChange"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleTableChange", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "handleTableChange"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSaveBtnClick", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSaveBtnClick"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "save", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "save"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleCancelBtnClick", [_dec10], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCancelBtnClick"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleResetBtnClick", [_dec11], Object.getOwnPropertyDescriptor(_class2.prototype, "handleResetBtnClick"), _class2.prototype)), _class2)) || _class);
export { RoleModal as default };